package lost_and_found.controller;

import java.sql.SQLException;

import lost_and_found.model.LostAndFoundService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import to.LostAndFoundListTO;
import to.LostAndFoundTO;
@Controller
public class LostAndFoundController {
	private LostAndFoundService service;

	public LostAndFoundController(LostAndFoundService service) {
		this.service = service;
	}
	@RequestMapping("/writeContent2.do")
	public ModelAndView writeContent(LostAndFoundTO lfto){
		ModelAndView mv = null;
		try {
			 service.writeContent(lfto);
			 mv = new ModelAndView("redirect:getContent2.do?no="+lfto.getNo());
			
		} catch (SQLException e) {			
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles", "error_message", "글등록 중 오류 발생 : "+e.getMessage());		
	
		}
		return mv;
	}
	
	@RequestMapping("/list2.do")
	public ModelAndView list(@RequestParam(required=false, defaultValue="1")String page) throws Exception{
		System.out.println(page);
		
		ModelAndView mv = null;
		//2. Business Logic 호출 ->model		
		try {
			if(page.equals("")){
				page = "1";
			}
			int pageNo = Integer.parseInt(page);
			LostAndFoundListTO lfto = service.getLostListByPage(pageNo);
			mv = new ModelAndView("lost/list.tiles","lfto",lfto);
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		return mv;
	}

	/*
	 * 글조회
	 */
	@RequestMapping("/getContent2.do")
	public ModelAndView getContent(int no){
		ModelAndView mv = null;
		try{
			//2. Business Logic 요청 - Model
			LostAndFoundTO lfto = service.getContentByNO(no);
			System.out.println(lfto);
			mv = new ModelAndView("lost/show_content.tiles", "lfto", lfto);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles", "error_message", "글 조회도중 오류 발생 : "+e.getMessage());
		}	
		return mv;
	}
	/**
	 * 수정폼 조회 처리
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyForm2.do")
	public ModelAndView modifyForm(int no){
		ModelAndView mv = null;
		try{
			//2. B.L 처리
			LostAndFoundTO lfto = service.getContentByNoForForm(no);
			mv = new ModelAndView("lost/modify_form.tiles","lfto", lfto);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles","error_message", "수정할 글 조회도중 오류발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 글 수정 처리 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyContent2.do")
	public ModelAndView modifyContent(LostAndFoundTO lfto) {
		ModelAndView mv = null;
		try {			
			//2. B.L 요청
			service.modifyContent(lfto);
			mv = new ModelAndView("lost/show_content.tiles", "lfto", lfto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles","error_message", "수정처리중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/deleteContent2.do")
	public ModelAndView deleteContent(int no, String page){
		ModelAndView mv = null;
		try {
			//2. BL
			service.deleteContentByNO(no);
			//mv = new ModelAndView("/list2.do");
			mv = list(page);
			//mv = list(request, response);
		}catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles","error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 답변 폼 조회 처리
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/replyForm2.do")
	public ModelAndView replyForm(int no){
		ModelAndView mv = null;
		try {
			//1. 요청 파라미터 조회 - no
			LostAndFoundTO lfto = service.getContentByNoForForm(no);
			mv = new ModelAndView("lost/reply_form.tiles", "lfto", lfto);
		}catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles", "error_message", "답변할 원본글 조회도중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 답변 처리
	 * @param request
	 * @param response
	 * @param bto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/replyContent2.do")
	public ModelAndView replyContent(LostAndFoundTO lfto, int page) throws Exception{
		ModelAndView mv = null;
		try{
			service.replyContent(lfto);
			//멱등처리
			mv = new ModelAndView("redirect:getContent2.do?no="+lfto.getNo()+"&page="+page);
		}catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("lost/error.tiles", "error_message", "답변 처리 중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/writeForm2.do")
	public String sendWrite(String url){
		url="lost/write_form.tiles";
		return url;
	}
	
}
